//
//  LCIPAddress.m
//  LendCash
//
//  Created by zhangtao on 2018/8/28.
//  Copyright © 2018年 zt. All rights reserved.
//

#import "LCIPAddress.h"

@implementation LCIPAddress

// 获取设备IP地址
+(NSString *)getIPAddress {
//    NSString *address = @"error";
//    struct ifaddrs *interfaces = NULL;
//    struct ifaddrs *temp_addr = NULL;
//    int success = 0;
//    // 检索当前接口,在成功时,返回0
//    success = getifaddrs(&interfaces);
//    if (success == 0) {
//        // 循环链表的接口
//        temp_addr = interfaces;
//        while(temp_addr != NULL) {
//            if(temp_addr->ifa_addr->sa_family == AF_INET) {
//                // 检查接口是否en0 wifi连接在iPhone上
//                if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) {
//                    // 得到NSString从C字符串
//                    address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
//                }
//            }
//            temp_addr = temp_addr->ifa_next;
//        }
//    }
//    // 释放内存
//    freeifaddrs(interfaces);
//    return address;
    
    NSURL *ipURL = [NSURL URLWithString:@"http://ip.taobao.com/service/getIpInfo.php?ip=myip"];
    NSData *data = [NSData dataWithContentsOfURL:ipURL];
    NSDictionary *ipDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; 
    NSString *ipStr = nil;
    if (ipDic && [ipDic[@"code"] integerValue] == 0) { //获取成功
        ipStr = ipDic[@"data"][@"ip"];
    }
    return (ipStr ? ipStr : @"");

}



@end
